package com.sywb.chuangyebao.contract;

import android.app.Activity;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.czt.mp3recorder.MP3Player;
import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.AnswerInfo;
import com.sywb.chuangyebao.bean.NewsInfo;
import com.sywb.chuangyebao.bean.NewsOwner;
import com.sywb.chuangyebao.bean.QuestionInfo;
import com.sywb.chuangyebao.bean.SuperInfo;
import com.sywb.chuangyebao.bean.VideoData;
import com.sywb.chuangyebao.utils.GlideUtils;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.utils.RegexUtils;
import com.sywb.chuangyebao.utils.Utils;
import com.sywb.chuangyebao.view.AnswerDetailActivity;
import com.sywb.chuangyebao.view.NewsDetailActivity;
import com.sywb.chuangyebao.view.QAndADetailActivity;
import com.sywb.chuangyebao.view.TopicActivity;
import com.sywb.chuangyebao.view.VideoDetailActivity;
import com.sywb.chuangyebao.widget.RichTextView;

import org.bining.footstone.adapter.BaseRecyclerMultiItemAdapter;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.log.Logger;
import org.bining.footstone.utils.TimeUtils;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public interface UserDynamicContract {

    interface View extends BaseRefreshContract.View {
        int getUid();
    }

    class Presenter extends BaseRefreshContract.Presenter<View> {

        protected FollowAdapter recyclerAdapter;

        private int uid;

        private int pageNumber = 1;

        @Override
        public void onStart() {
            super.onStart();
            uid = mView.getUid();

            recyclerAdapter = new FollowAdapter(mActivity, mView);
            setRecyclerAdapter(recyclerAdapter);
            pullToRefreshView.setRefreshEnable(false);
            recyclerAdapter.setPageSize(1);

            initData();
        }

        @Override
        public void initData() {
            onStartAsync();
            getDynamicList();
        }

        @Override
        public void onStartRefresh() {
            pageNumber = 1;
            getDynamicList();
        }

        @Override
        public void onStartLoadMore() {
            pageNumber++;
            getDynamicList();
        }

        private void getDynamicList() {
            HttpUtils.dynamicListById(uid, pageNumber, new HttpCallback<List<SuperInfo>>() {

                @Override
                public void onSuccess(List<SuperInfo> superInfos) {
                    if (superInfos != null) {
                        for (SuperInfo superInfo : superInfos) {
                            superInfo.setItemType(superInfo.item_style);
                        }
                    }
                    if (pageNumber == 1) {
                        onEndRefresh();
//                        if (superInfos != null && superInfos.size() > 0) {
//                            recyclerAdapter.setFooterView(R.layout.layout_footer);
//                        } else {
//                            recyclerAdapter.setFooterView(null);
//                        }
                        recyclerAdapter.setFooterView(null);
                        recyclerAdapter.clearDatas();
                    } else {
                        if (superInfos != null && superInfos.size() > 0) {
                            recyclerAdapter.setFooterView(R.layout.layout_footer);
                        } else {
                            recyclerAdapter.setFooterView(null);
                        }
                    }
                    if (superInfos != null) {
                        recyclerAdapter.notifyDataChangedAfterLoadMore(superInfos);
                    }
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    if (pageNumber == 1) {
                        onEndRefresh();
                    } else {
                        onEndLoadMore();
                    }
                    showMessage(strMsg);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                    isNoData();
                }
            });
        }

        @Override
        public void onRecyclerItemClick(int i) {
            SuperInfo superInfo = recyclerAdapter.getItem(i);
            switch (superInfo.item_style) {
                case Constants.NEWS_TEXT:
                case Constants.NEWS_RIGHT_PIC_VIDEO:
                case Constants.NEWS_CENTER_SINGLE_PIC:
                case Constants.NEWS_THREE_PICS:
                    NewsInfo newsInfo = JSON.parseObject(superInfo.content, NewsInfo.class);
                    newsInfo.hits += 1;
                    newsInfo.count.hits += 1;
                    superInfo.content = JSON.toJSONString(newsInfo);
                    recyclerAdapter.notifyDataSetChanged();
                    mView.advance(NewsDetailActivity.class, newsInfo.item_id);
                    break;
                case Constants.QANDA_TEXT:
                case Constants.QANDA_PIC_ONE:
                case Constants.QANDA_PIC_THREE:
                case Constants.QANDA_MEDIA:
                    QuestionInfo question = JSON.parseObject(superInfo.content, QuestionInfo.class);
                    mView.advance(QAndADetailActivity.class, question.item_id);
                    break;
                case Constants.VIDEO_LIST:
                    VideoData videoInfo = JSON.parseObject(superInfo.content, VideoData.class);
                    mView.advance(VideoDetailActivity.class, videoInfo.item_id);
                    break;
                case Constants.ANSWER_LIST:
                    AnswerInfo answerInfo = JSON.parseObject(superInfo.content, AnswerInfo.class);
                    mView.advance(AnswerDetailActivity.class, answerInfo.item_id);
                    break;
            }
        }

        @Override
        public void onRecyclerItemChildClick(android.view.View view, int i) {
            SuperInfo superInfo = recyclerAdapter.getItem(i);
            switch (superInfo.item_style) {
                case Constants.NEWS_TEXT:
                case Constants.NEWS_RIGHT_PIC_VIDEO:
                case Constants.NEWS_CENTER_SINGLE_PIC:
                case Constants.NEWS_THREE_PICS:
                    NewsInfo newsInfo = JSON.parseObject(superInfo.content, NewsInfo.class);
                    switch (view.getId()) {
                        case R.id.iv_face:
                        case R.id.iv_user_type:
                        case R.id.tv_collection_name:
                        case R.id.tv_collection_time:
                            Utils.goUserIndex(mView, newsInfo.owner.uid);
//                    mView.advance(UserIndexActivity.class, newsInfo.item_id);
                            break;
                        case R.id.tv_question:
                            newsInfo.hits += 1;
                            newsInfo.count.hits += 1;
                            superInfo.content = JSON.toJSONString(newsInfo);
                            recyclerAdapter.notifyDataSetChanged();
                            mView.advance(NewsDetailActivity.class, newsInfo.item_id);
                            break;
                    }
                    break;
                case Constants.QANDA_TEXT:
                case Constants.QANDA_PIC_ONE:
                case Constants.QANDA_PIC_THREE:
                case Constants.QANDA_MEDIA:
                    QuestionInfo question = JSON.parseObject(superInfo.content, QuestionInfo.class);
                    switch (view.getId()) {
                        case R.id.iv_face:
                        case R.id.iv_user_type:
                        case R.id.tv_collection_name:
                        case R.id.tv_collection_time:
                            Utils.goUserIndex(mView, question.owner.uid);
//                            mView.advance(UserIndexActivity.class, question.owner.uid);
                            break;
                        case R.id.richTextView:
                            mView.advance(QAndADetailActivity.class, question.item_id);
                            break;
                        case R.id.iv_img0:
                            Utils.goBigPic(mActivity, 0, question.pic);
                            break;
                        case R.id.iv_img1:
                            Utils.goBigPic(mActivity, 1, question.pic);
                            break;
                        case R.id.iv_img2:
                            Utils.goBigPic(mActivity, 2, question.pic);
                            break;
                        case R.id.iv_media_type:
                            //播放语音
                            if (currentAudioQuestion == null || currentAudioPosition != i) {
                                currentAudioPosition = i;
                                currentAudioQuestion = question;
                            }
                            playAudio(question.audio);
                            recyclerAdapter.notifyDataSetChanged();
                            break;

                    }
                    break;
                case Constants.VIDEO_LIST:
                    VideoData videoInfo = JSON.parseObject(superInfo.content, VideoData.class);
                    switch (view.getId()) {
                        case R.id.iv_face:
                        case R.id.iv_user_type:
                        case R.id.tv_collection_name:
                        case R.id.tv_collection_time:
//                    mView.advance(UserIndexActivity.class, videoInfo.item_id);
                            Utils.goUserIndex(mView, videoInfo.owner.uid);
                            break;
                        case R.id.tv_question:
                            mView.advance(VideoDetailActivity.class, videoInfo.item_id);
                            break;
                    }

                    break;
                case Constants.ANSWER_LIST:
                    AnswerInfo answerInfo = JSON.parseObject(superInfo.content, AnswerInfo.class);
                    switch (view.getId()) {
                        case R.id.iv_face:
                        case R.id.iv_user_type:
                        case R.id.tv_collection_name:
                        case R.id.tv_collection_time:
//                            mView.advance(UserIndexActivity.class, answerInfo.owner.uid);
                            Utils.goUserIndex(mView, answerInfo.owner.uid);
                            break;
                        case R.id.richTextView:
                            mView.advance(AnswerDetailActivity.class, answerInfo.item_id);
                            break;
                        case R.id.ll_question:
                            mView.advance(QAndADetailActivity.class, answerInfo.question_id);
                            break;
                    }
                    break;
            }
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            if (mp3Player != null) {
                mp3Player.stopPlay();
                mp3Player = null;
            }
        }

        private MP3Player mp3Player;
        private boolean isPlaying;
        private String playingAudioUrl;

        private void playAudio(String currentAudioUrl) {
            //播放语音
            if (mp3Player == null) {
                mp3Player = MP3Player.getInstance();
                mp3Player.setOnMP3PlayerListener(new MP3Player.OnMP3PlayerListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        Logger.e("MediaPlayer onCompletion:");
                        currentAudioQuestion.progress = 0;
                        playStop();
                    }

                    @Override
                    public void onSeekComplete(MediaPlayer mediaPlayer) {
                        Logger.e("MediaPlayer onSeekComplete:");
                    }

                    @Override
                    public void onError(MediaPlayer mediaPlayer) {
                        onCompletion(mediaPlayer);
                        showMessage("播放错误,请稍候再试");
                    }
                });

                mp3Player.setOnMP3PlayerStartListener(new MP3Player.onMP3PlayerStartListener() {
                    @Override
                    public void onStart(MediaPlayer mediaPlayer) {
                        Logger.e("duration:" + mp3Player.getMediaPlayer().getDuration());
//                        seekbar.setMax(mp3Player.getMediaPlayer().getDuration());
//                        tvAudioTime.setText(DateTimeUtils.getMediaTimeNumber(seekbar.getMax() / 1000));
                        currentAudioQuestion.max = mp3Player.getMediaPlayer().getDuration();
                        recyclerAdapter.notifyDataSetChanged();
                        startTimer();
                    }
                });

                mp3Player.startPlay(currentAudioUrl, null);
                isPlaying = true;
                playingAudioUrl = currentAudioUrl;
                return;
            }
            Logger.e("是否点击同一个：" + currentAudioUrl.equals(playingAudioUrl) + "是否在播：" + isPlaying);

            if (currentAudioUrl.equals(playingAudioUrl)) {
                if (isPlaying) {
                    mp3Player.pausePlay();
                    playStop();
                } else {
                    mp3Player.restart();
                    isPlaying = true;
                    startTimer();
                }
            } else {
                if (isPlaying) {
                    mp3Player.stopPlay();
                }
                currentAudioQuestion.progress = 0;
                playStop();
                mp3Player.startPlay(currentAudioUrl, null);
                isPlaying = true;
                startTimer();
            }
            playingAudioUrl = currentAudioUrl;
        }

        public void playStop() {
            isPlaying = false;
            if (recyclerAdapter != null && mp3Player != null) {
                recyclerAdapter.notifyDataSetChanged();
            }
            stopTimer();
        }
        private Timer mTimer;
        private boolean isChanging = false;//互斥变量，防止定时器与SeekBar拖动时进度冲突


        private void startTimer() {
            if (currentAudioQuestion.progress > 0) {
                mp3Player.getMediaPlayer().seekTo(currentAudioQuestion.progress);
            }

            //----------定时器记录播放进度---------//
            isChanging = false;
            mTimer = new Timer();
            TimerTask mTimerTask = new TimerTask() {
                @Override
                public void run() {
                    if (isChanging) {
                        return;
                    }

                    try {
                        if (mp3Player.getMediaPlayer() == null) {
                            return;
                        }

//                        seekbar.setProgress(mp3Player.getMediaPlayer().getCurrentPosition());
                        currentAudioQuestion.progress = mp3Player.getMediaPlayer().getCurrentPosition();

//                        tvCurrentTime.setText(DateTimeUtils.getMediaTimeNumber(seekBar.getProgress() / 1000));
                        mActivity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                recyclerAdapter.notifyDataSetChanged();
                            }
                        });
                        Logger.e("currentPosition:" + mp3Player.getMediaPlayer().getCurrentPosition());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            mTimer.schedule(mTimerTask, 0, 500);
        }


        private void stopTimer() {
            if (mp3Player != null) {
                mp3Player.pause();
            }

            if (mTimer != null) {
                isChanging = true;
                mTimer.cancel();
                Logger.e("stopTimer");
            }
        }

        private QuestionInfo currentAudioQuestion;
        private int currentAudioPosition;

        class FollowAdapter extends BaseRecyclerMultiItemAdapter<SuperInfo> {

            private Activity mActivity;
            private View mView;

            public FollowAdapter(Activity activity, View view) {
                super(activity, null);
                this.mActivity = activity;
                this.mView = view;
                addItemType(Constants.NEWS_TEXT, R.layout.item_dynamic);//文章纯文字布局
                addItemType(Constants.NEWS_RIGHT_PIC_VIDEO, R.layout.item_dynamic);//文章右侧小图布局
                addItemType(Constants.NEWS_CENTER_SINGLE_PIC, R.layout.item_dynamic);//文章居中大图布局
                addItemType(Constants.NEWS_THREE_PICS, R.layout.item_dynamic);//文章三张图片布局
                addItemType(Constants.QANDA_TEXT, R.layout.item_user_dynamic_text);//问答纯文字布局
                addItemType(Constants.QANDA_PIC_ONE, R.layout.item_user_dynamic_pic_one);//问答右侧小图布局
                addItemType(Constants.QANDA_PIC_THREE, R.layout.item_user_dynamic_pic_three);//问答多图布局
                addItemType(Constants.QANDA_MEDIA, R.layout.item_user_dynamic_media);//问答多媒体布局
                addItemType(Constants.VIDEO_LIST, R.layout.item_dynamic);//视频布局
                addItemType(Constants.ANSWER_LIST, R.layout.item_follow_answer);//回答布局
            }

            @Override
            protected int getDefItemViewType(int position) {
                if (position >= getDataCount()) {
                    return 0x00000333;
                }
                return super.getDefItemViewType(position);
            }

            @Override
            protected void setMultiData(ViewHolderHelper viewHolderHelper, int i, SuperInfo superInfo) {
                if (superInfo.item_style >= Constants.NEWS_TEXT && superInfo.item_style <= Constants.NEWS_THREE_PICS) {

                    NewsInfo newsInfo = JSON.parseObject(superInfo.content, NewsInfo.class);

                    if (TextUtils.isEmpty(newsInfo.title)) {
                        return;
                    }

                    setHeadView(viewHolderHelper, newsInfo.owner, newsInfo.publish_time, superInfo.msg);

                    viewHolderHelper.setText(R.id.tv_question, newsInfo.title);

                    viewHolderHelper.setItemChildClickListener(R.id.tv_question);

                    viewHolderHelper.setVisibility(R.id.tv_recommend_red, false);
                    if (newsInfo.count != null) {
                        viewHolderHelper.setVisibility(R.id.tv_bottom_left, true);
                        viewHolderHelper.setText(R.id.tv_bottom_left, newsInfo.count.getHitsString() + " · " + RegexUtils.getFormatNum(newsInfo.count.thumb_up) + "赞 · " + RegexUtils.getFormatNum(newsInfo.count.comment) + "评论");
                    } else {
                        viewHolderHelper.setVisibility(R.id.tv_bottom_left, false);
                    }
                    viewHolderHelper.setVisibility(R.id.tv_bottom_right, false);

                    if (superInfo.getItemType() != Constants.NEWS_TEXT) {
                        viewHolderHelper.setVisibility(R.id.rl_media, true);
                        viewHolderHelper.setVisibility(R.id.iv_media_type, false);
                        ImageView newsFace = viewHolderHelper.getView(R.id.iv_media);
                        GlideUtils.getNormalIamgeToUrl(mActivity, newsInfo.thumbnails.get(0).trim(), newsFace, R.drawable.image_def);
                    } else {
                        viewHolderHelper.setVisibility(R.id.rl_media, false);
                    }
                } else if (superInfo.item_style >= Constants.QANDA_TEXT && superInfo.item_style <= Constants.QANDA_MEDIA) {
                    QuestionInfo questionInfo = JSON.parseObject(superInfo.content, QuestionInfo.class);
                    questionInfo.setTopic();

                    setHeadView(viewHolderHelper, questionInfo.owner, questionInfo.create_time, superInfo.msg);

                    viewHolderHelper.setVisibility(R.id.tv_recommend_red, false);
                    viewHolderHelper.setText(R.id.tv_bottom_left, questionInfo.count.getAnswer());
                    viewHolderHelper.setVisibility(R.id.tv_bottom_right, false);

                    //富文本
                    RichTextView richTextView = viewHolderHelper.getView(R.id.richTextView);
                    richTextView.setDeviationDP(R.dimen.dp_32);

                    switch (superInfo.getItemType()) {
                        case Constants.QANDA_TEXT:
                            break;
                        case Constants.QANDA_PIC_ONE:
                            //设置图片（一张）
                            ImageView imageView = viewHolderHelper.getView(R.id.iv_img);
                            GlideUtils.getNormalIamgeToUrl(mActivity, questionInfo.pic.get(0), imageView);

                            richTextView.setDeviationDP(R.dimen.dp_172);
                            break;
                        case Constants.QANDA_PIC_THREE:
                            if (questionInfo.pic != null) {
                                if (questionInfo.pic.size() > 0) {
                                    viewHolderHelper.setVisibility(R.id.iv_img0, true);
                                    viewHolderHelper.setItemChildClickListener(R.id.iv_img0);
                                    ImageView imageView0 = viewHolderHelper.getView(R.id.iv_img0);
                                    GlideUtils.getNormalIamgeToUrl(mActivity, questionInfo.pic.get(0), imageView0);
                                } else {
                                    viewHolderHelper.setVisibility(R.id.iv_img0, false);
                                    viewHolderHelper.setVisibility(R.id.iv_img1, false);
                                    viewHolderHelper.setVisibility(R.id.iv_img2, false);
                                    break;
                                }
                                if (questionInfo.pic.size() > 1) {
                                    viewHolderHelper.setVisibility(R.id.iv_img1, true);
                                    viewHolderHelper.setItemChildClickListener(R.id.iv_img1);
                                    ImageView imageView1 = viewHolderHelper.getView(R.id.iv_img1);
                                    GlideUtils.getNormalIamgeToUrl(mActivity, questionInfo.pic.get(1), imageView1);
                                } else {
                                    viewHolderHelper.setVisibility(R.id.iv_img1, false);
                                    viewHolderHelper.setVisibility(R.id.iv_img2, false);
                                    break;
                                }
                                if (questionInfo.pic.size() > 2) {
                                    viewHolderHelper.setVisibility(R.id.iv_img2, true);
                                    viewHolderHelper.setItemChildClickListener(R.id.iv_img2);
                                    ImageView imageView2 = viewHolderHelper.getView(R.id.iv_img2);
                                    GlideUtils.getNormalIamgeToUrl(mActivity, questionInfo.pic.get(2), imageView2);
                                } else {
                                    viewHolderHelper.setVisibility(R.id.iv_img2, false);
                                }
                            }
                            break;
                        case Constants.QANDA_MEDIA:
                            //设置语音背景图片(用户头像)
//                        ImageView mediaImage = viewHolderHelper.getView(R.id.iv_media);
//                        GlideUtils.getNormalIamgeToUrl(mActivity, questionInfo.owner.getShowAvatar(), mediaImage);
//                        //设置语音时长
//                        viewHolderHelper.setText(R.id.tv_audio_time, questionInfo.audioLength);
                            if (currentAudioQuestion != null && currentAudioQuestion.item_id == questionInfo.item_id && currentAudioPosition == i) {
                                Utils.setQuestionAudioItemView(viewHolderHelper, mActivity, currentAudioQuestion, playingAudioUrl, mp3Player, isPlaying);
                            } else {
                                Utils.setQuestionAudioItemView(viewHolderHelper, mActivity, questionInfo, playingAudioUrl, mp3Player, isPlaying);
                            }
                            break;
                    }

                    richTextView.setDataPostion(i);
                    richTextView.setExpandView(true);
                    richTextView.setExpandedOpen(false);
                    richTextView.setRichText(questionInfo.title, questionInfo.richInfoList);
                    richTextView.setOnLinkClickListener(new RichTextView.OnLinkClickListener() {
                        @Override
                        public void onAtClick(RichTextView richTextView, Object userId) {
//                            mView.advance(UserIndexActivity.class, userId);
                            Utils.goUserIndex(mView, (Integer) userId);
                        }

                        @Override
                        public void onTopicClick(RichTextView richTextView, Object topicId) {
                            mView.advance(TopicActivity.class, topicId);
                        }

                        @Override
                        public void onEmojiClick(RichTextView richTextView, Object emoji) {

                        }

                        @Override
                        public void onUrlClick(RichTextView richTextView, Object url) {

                        }

                        @Override
                        public void onAllClick(RichTextView richTextView) {
                            SuperInfo superInfo = getItem(richTextView.getDataPostion());
                            QuestionInfo questionInfo = JSON.parseObject(superInfo.content, QuestionInfo.class);
                            mView.advance(QAndADetailActivity.class, questionInfo.item_id);
                        }
                    });

                    viewHolderHelper.setItemChildClickListener(R.id.richTextView);

                } else if (superInfo.item_style == Constants.VIDEO_LIST) {
                    VideoData videoInfo = JSON.parseObject(superInfo.content, VideoData.class);

                    setHeadView(viewHolderHelper, videoInfo.owner, videoInfo.publish_time, superInfo.msg);

                    viewHolderHelper.setText(R.id.tv_question, videoInfo.title);

                    viewHolderHelper.setItemChildClickListener(R.id.tv_question);

                    viewHolderHelper.setVisibility(R.id.rl_media, true);
                    viewHolderHelper.setVisibility(R.id.iv_media_type, true);
                    ImageView newsFace = viewHolderHelper.getView(R.id.iv_media);
                    GlideUtils.getNormalIamgeToUrl(mActivity, videoInfo.logo, newsFace, R.drawable.image_def);

                    viewHolderHelper.setVisibility(R.id.tv_recommend_red, false);
                    if (videoInfo.count != null) {
                        viewHolderHelper.setVisibility(R.id.tv_bottom_left, true);
                        viewHolderHelper.setText(R.id.tv_bottom_left, videoInfo.count.getPlayTimesTotal() + " · " + RegexUtils.getFormatNum(videoInfo.count.thumb_up) + "赞 · " + RegexUtils.getFormatNum(videoInfo.count.comment) + "评论");
                    } else {
                        viewHolderHelper.setVisibility(R.id.tv_bottom_left, false);
                    }
                    viewHolderHelper.setVisibility(R.id.tv_bottom_right, false);
                } else {
                    AnswerInfo answerInfo = JSON.parseObject(superInfo.content, AnswerInfo.class);

                    setHeadView(viewHolderHelper, answerInfo.owner, answerInfo.create_time, superInfo.msg);

                    //设置富文本
                    RichTextView richTextView = viewHolderHelper.getView(R.id.richTextView);
                    richTextView.setDataPostion(i);
                    richTextView.setExpandView(true);
                    richTextView.setExpandedOpen(false);
                    richTextView.setRichText(answerInfo.content, answerInfo.richInfoList);
                    richTextView.setOnLinkClickListener(new RichTextView.OnLinkClickListener() {
                        @Override
                        public void onAtClick(RichTextView richTextView, Object userId) {
//                            mView.advance(UserIndexActivity.class, userId);
                            Utils.goUserIndex(mView, (Integer) userId);
                        }

                        @Override
                        public void onTopicClick(RichTextView richTextView, Object topicId) {
                            mView.advance(TopicActivity.class, topicId);
                        }

                        @Override
                        public void onEmojiClick(RichTextView richTextView, Object emoji) {

                        }

                        @Override
                        public void onUrlClick(RichTextView richTextView, Object url) {

                        }

                        @Override
                        public void onAllClick(RichTextView richTextView) {
                            SuperInfo superInfo = getItem(richTextView.getDataPostion());
                            AnswerInfo answerInfo = JSON.parseObject(superInfo.content, AnswerInfo.class);
                            mView.advance(AnswerDetailActivity.class, answerInfo.item_id);
                        }
                    });

                    viewHolderHelper.setItemChildClickListener(R.id.richTextView);

                    viewHolderHelper.setVisibility(R.id.tv_recommend_red, false);
                    viewHolderHelper.setText(R.id.tv_bottom_left, RegexUtils.getFormatNum(answerInfo.count.thumb_up) + "赞 · " + RegexUtils.getFormatNum(answerInfo.count.comment) + "评论");
                    viewHolderHelper.setVisibility(R.id.tv_bottom_right, false);

                    if (answerInfo.question != null) {
                        answerInfo.question.setItemType();
                        answerInfo.question.setTopic();

                        ImageView mediaBg = viewHolderHelper.getView(R.id.iv_media);
                        viewHolderHelper.setVisibility(R.id.rl_media, true);
                        switch (answerInfo.question.getItemType()) {
                            case Constants.QANDA_TEXT:
                                viewHolderHelper.setVisibility(R.id.rl_media, false);
                                break;
                            case Constants.QANDA_PIC_ONE:
                            case Constants.QANDA_PIC_THREE:
                                viewHolderHelper.setVisibility(R.id.iv_media_type, false);
                                GlideUtils.getNormalIamgeToUrl(mActivity, answerInfo.question.pic.get(0), mediaBg, R.drawable.image_def);
                                break;
                            case Constants.QANDA_MEDIA:
                                viewHolderHelper.setVisibility(R.id.iv_media_type, true);
                                GlideUtils.getNormalIamgeToUrl(mActivity, answerInfo.question.owner.getShowAvatar(), mediaBg, R.drawable.head_image);
                                break;
                        }

                        //设置富文本
                        RichTextView tvQuestion = viewHolderHelper.getView(R.id.tv_question);
                        tvQuestion.setExpandView(false);
                        tvQuestion.setRichText(answerInfo.question.title, answerInfo.question.richInfoList);
                        tvQuestion.setOnLinkClickListener(new RichTextView.OnLinkClickListener() {
                            @Override
                            public void onAtClick(RichTextView richTextView, Object userId) {
//                                mView.advance(UserIndexActivity.class, userId);
                                Utils.goUserIndex(mView, (Integer) userId);
                            }

                            @Override
                            public void onTopicClick(RichTextView richTextView, Object topicId) {
                                mView.advance(TopicActivity.class, topicId);
                            }

                            @Override
                            public void onEmojiClick(RichTextView richTextView, Object emoji) {

                            }

                            @Override
                            public void onUrlClick(RichTextView richTextView, Object url) {

                            }

                            @Override
                            public void onAllClick(RichTextView richTextView) {
                            }
                        });

                        viewHolderHelper.setItemChildClickListener(R.id.ll_question);
                    }
                }
            }

            private void setHeadView(ViewHolderHelper viewHolderHelper, NewsOwner owner, long publishTime, String action) {
                ImageView imageFace = viewHolderHelper.getView(R.id.iv_face);
                GlideUtils.getCircleImageToUrl(mActivity, owner.getShowAvatar(), imageFace, R.drawable.head_image);
                Utils.setUserRoleIcon((ImageView) viewHolderHelper.getView(R.id.iv_user_type), owner.user_role);
                viewHolderHelper.setText(R.id.tv_collection_name, owner.getShowName());

                viewHolderHelper.setText(R.id.tv_collection_time, TimeUtils.transformDate10(publishTime));

                viewHolderHelper.setVisibility(R.id.tv_recommend, false);

                viewHolderHelper.setVisibility(R.id.tv_operate, true);
                viewHolderHelper.setText(R.id.tv_operate, action);

//                viewHolderHelper.setItemChildClickListener(R.id.iv_face);
//                viewHolderHelper.setItemChildClickListener(R.id.iv_user_type);
//                viewHolderHelper.setItemChildClickListener(R.id.tv_collection_name);
//                viewHolderHelper.setItemChildClickListener(R.id.tv_collection_time);
            }
        }

    }


}

